<?php

class video_collection implements IteratorAggregate {

	protected $_collection = array();
	
	/**
	 * 
	 * @param array of videos $collection
	 * @return video_collection
	 */
	public function __construct($collection = array()) {
		$this->_collection = $collection;
		return $this;
	}
	
	public function getIterator() {
        return new ArrayIterator($this->_collection);
    }
	
	public function addItem(video $video) {
		$this->_collection[$video->getId()] = $video;
	}
	
	public function mergeItems(video_collection $videos) {
		foreach($videos as $video) {
			if ($this->has($video->getId())) {
				$this->unsetVideo($video->getId());
			}
			$this->addItem($video);
		}
	}
	
	public function has($id) {
		return isset($this->_collection[$id]);
	}
	
	public function unsetVideo($id) {
		unset($this->_collection[$id]);
	}
	
	public function limitElements($count, $fromEnd = false) {
		if ($fromEnd) {
			$count = (-$count);
		}
		if ($count < 0) {
			// set new array to removed portion of collection, counting from the end
			$this->_collection = array_splice($this->_collection, $count);
		} else {
			// removes all items from array, starting at count 
			array_splice($this->_collection, $count);
		}
	}
	
	public function get($id) {
		if ($this->has($id)) {
			return $this->_collection[$id];
		} else {
			return false;
		}
	}
	
	public function getFirst() {
		return array_pop($this->_collection);
	}
	
	public function getDataArray($includeMeta = false, $getDynamicMeta = true, $addExtendedInfo = true) {
		return model_helper::getAllDataFromVideoArray($this->_collection, $includeMeta, $getDynamicMeta, $addExtendedInfo);
	}
	
	public function count() {
		return count($this->_collection);
	}
	
	public function randomKeys($count = 1) {
		if (count($this->_collection) < $count) {
			return false;
		}
		return array_rand($this->_collection, $count);
	}
	
}

?>